package cn.cloudplug.instar_ec.accessional.picture;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import java.io.File;
import java.util.List;

import cn.cloudplug.core.delegate.ScentDelegate;
import cn.cloudplug.core.util.MyUtil;
import cn.cloudplug.instar_ec.R;

/**
 * Created by pc on 2018/1/23.
 */

public class PictureDelegate extends ScentDelegate implements PicControlMode.OnChangeModeListener,AdapterView.OnItemClickListener{

    private GridView mGridView;
    private List<File> list;
    private PicControlMode picControlMode;
    private PictureAdapter mAdapter;

    @Override
    protected Object setContent() {
        return mGridView=new GridView(getContext());
    }

    public PictureDelegate setData(List<File> list){
        this.list=list;
        return this;
    }

    public PictureDelegate setMode(PicControlMode picControlMode){
        this.picControlMode=picControlMode;
        this.picControlMode.addOnChangeModeListener(this);
        return this;
    }

    @Override
    protected void onBindView(Bundle savedInstanceState, View view) {
        mGridView.setNumColumns(2);
        int pix = MyUtil.DipToPixels(getContext(),10);
        mGridView.setHorizontalSpacing(pix);
        mGridView.setVerticalSpacing(pix);
        mGridView.setPadding(pix,pix,pix,0);
        mGridView.setOnItemClickListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        mAdapter = new PictureAdapter(getContext(),picControlMode,list, R.mipmap.icon_camera);
        mGridView.setAdapter(mAdapter);
    }

    @Override
    public void onChangeModeListener(int mode) {
        if(mAdapter!=null)mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(position>=1)
        picControlMode.getScaleImageView().files(list).show(position-1);
    }
}
